<?php

	require_once('main.class.php');
	require_once('_db/db.class.php');
	require_once('debug.class.php');
	#doc
	#	classname:	Broadcast
	#	scope:		PUBLIC
	#
	#/doc

	class Broadcast {
		#	internal variables

		/**
		 * @var static $_instance
		 * Singleton pattern
		 */
		public static $_instance = NULL;

		/**
		 * @var string $_query
		 * запрос в БД
		 */
		private $_query			 = NULL;

		/**
		 * @var array $_fetch
		 * результат запроса, массив
		 */
		private $_fetch 	  = array();

		#	Singleton pattern
		public static function getInstance() {
			if (is_null(self::$_instance)) {
				$class = __CLASS__;
				self::$_instance = new $class();
			}
			return self::$_instance;
		}
		###

		public function print_info($message) {
		  if (debug::endebug()) {
		    print "<p class='debug'>Глобальные сообщения - информация</p>";
		    debug::d($message);
		  }
			$t = explode("\n", $message);
			for ($i=0; $i <= count ($t); $i++) {
				print "<p>".$t[$i]."</p>";
			}
		}

		public function print_error($message) {
		  if (debug::endebug()) {
		    print "<p class='debug'>Глобальные сообщения - ошибки</p>";
		    debug::d($message);
		  }
			$t = explode("\n", $message);
			for ($i=0; $i<=count($t); $i++) {
				print "<p>".$t[$i]."</p>";
			}
			exit();
		}

		public function insert_public($message) {
			Core::init();
			$this->_query = "INSERT INTO `global_messages` (`user`, `text`, `date`, `read`) VALUES ('$message', ".time().", '0')";
			if (debug::endebug()) {
			   print "<p class='debug'>Вставка глобального сообщения</p>";
			   debug::d($this->_query);
		  }
			db_query::query($this->_query);
			unset($this->_query);
		}

		public function print_public() {
			Core::init();
			$this->_query = "SELECT * FROM `global_messages` WHERE `read` = '0' AND `user` = '$_SESSION[name]'";
			$this->_fetch = db_query::query($this->_query);
			if (debug::endebug()) {
			  print "<p class='debug'>Вывод глобального сообщения</p>";
			  debug::d($this->_query);
			  debug::d($this->_fetch);
		  }
			for ($i=0; $i<=count($this->_fetch); $i++) {
				print $this->_fetch[$i][$text];
			}
			unset($this->_query); $this->_query = "DELETE FROM `global_messages` WHERE `read` = '1'";
			if (DB::getInstance()->query($this->_query)) {

				return true;
			} else {
				error::write("Ошибка при удалении глобального сообщения: ".mysql_error());
				return false;
			}

		}

	}
	###

?>

